package mspdt.ubzuuch.mn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;

public class SearchActivity extends Activity{
	
	private Spinner category, duureg, room1, room2, type;
	private EditText price1, price2, talbai1, talbai2, yavts;
	private ImageView search;
	private LinearLayout main_ll, talbai_ll, roomnum_ll, yavts_ll, type_ll;
	
	private String[] categories = {"Худалдаа", "Түрээс","Шинэ орон сууц"};
	private String[] duureguud = {"Бүгд", "Баянгол","Баянзүрх","Сүхбаатар","Сонгинохайрхан","Чингэлтэй","Хан-Уул"};
	private String[] rooms1={"1","2","3","4"};
	private String[] rooms2={"2","3","4","4+"};
	private String[] types={"Орон сууц", "Барилга","Хашаа байшин","Зуслан"};
	private String room1_t="roomnum1=", room2_t="roomnum2=", talbai1_t="sqrmin=",talbai2_t="sqrmax=",
			minprice="minprice=",maxprice="maxprice=",yavts_t="yabts=",duureg_t="duureg=", category_t="category=", type_t="type=";
	@Override
	public void onCreate(Bundle bundle)
	{
		super.onCreate(bundle);
		setContentView(R.layout.search_view);
		main_ll = (LinearLayout)findViewById(R.id.search_main_ll);
		talbai_ll = (LinearLayout)findViewById(R.id.search_ll_talbai);
		roomnum_ll = (LinearLayout)findViewById(R.id.search_ll_oroonum);
		yavts_ll = (LinearLayout)findViewById(R.id.search_ll_yavts);
		type_ll = (LinearLayout)findViewById(R.id.search_ll_type);
		main_ll.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				overridePendingTransition(R.anim.no_anim, R.anim.top_out);
			}
		});
		
		ArrayAdapter<String> cat_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
		cat_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		category=(Spinner)findViewById(R.id.search_spinner_category);
		category.setAdapter(cat_adapter);
		category.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				if(arg2==0)
				{
					category_t="category=1";
					talbai_ll.setVisibility(View.GONE);
					yavts_ll.setVisibility(View.GONE);
					roomnum_ll.setVisibility(View.VISIBLE);
					type_ll.setVisibility(View.VISIBLE);
				}
				else if(arg2==1)
				{
					category_t="category=2";
					talbai_ll.setVisibility(View.VISIBLE);
					yavts_ll.setVisibility(View.GONE);
					roomnum_ll.setVisibility(View.VISIBLE);
					type_ll.setVisibility(View.VISIBLE);
				}
				else if(arg2==2)
				{
					category_t="category=3";
					talbai_ll.setVisibility(View.GONE);
					yavts_ll.setVisibility(View.VISIBLE);
					roomnum_ll.setVisibility(View.VISIBLE);
					type_ll.setVisibility(View.GONE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		ArrayAdapter<String> type_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, types);
		type_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		type=(Spinner)findViewById(R.id.search_spinner_type);
		type.setAdapter(type_adapter);
		type.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				if(arg2==0)
				{
					roomnum_ll.setVisibility(View.VISIBLE);
					talbai_ll.setVisibility(View.GONE);
				}
				else
				{
					talbai_ll.setVisibility(View.VISIBLE);
					roomnum_ll.setVisibility(View.GONE);
				}
					
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		ArrayAdapter<String> duureg_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, duureguud);
		duureg_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		duureg=(Spinner)findViewById(R.id.search_spinner_duureg);
		duureg.setAdapter(duureg_adapter);
		
		
		ArrayAdapter<String> room1_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, rooms1);
		room1_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		room1=(Spinner)findViewById(R.id.search_spinner_room1);
		room1.setAdapter(room1_adapter);
		
		ArrayAdapter<String> room2_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, rooms2);
		room2_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		room2=(Spinner)findViewById(R.id.search_spinner_room2);
		room2.setAdapter(room2_adapter);
		
		price1=(EditText)findViewById(R.id.search_et_price1);
		price2=(EditText)findViewById(R.id.search_et_price2);
		talbai1=(EditText)findViewById(R.id.search_et_talbai1);
		talbai2=(EditText)findViewById(R.id.search_et_talbai2);
		yavts=(EditText)findViewById(R.id.search_et_yavts);
		
		search=(ImageView)findViewById(R.id.search_search);
		search.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				room1_t="roomnum1="; room2_t="roomnum2="; talbai1_t="sqrmin=";talbai2_t="sqrmax=";
						minprice="minprice=";maxprice="maxprice=";yavts_t="yabts=";duureg_t="duureg=";type_t="type=";
				String url = "http://202.131.237.185/ubzuuch/web/banner/search?";
				url=url+category_t;
				if(roomnum_ll.isShown())
				{
					room1_t=room1_t+room1.getSelectedItem().toString()+" өрөө";
					room2_t=room2_t+room2.getSelectedItem().toString()+" өрөө";
					//room2_t.replace("4+", "4");
					url=url+"&"+room1_t+"&"+room2_t.replace("4+", "4");
				}
//				else
//				{
//	//				room1_t=room1_t+"0 өрөө";
//	//				room2_t=room2_t+"0 өрөө";
//	//				url=url+"&"+room1_t+"&"+room2_t;
//				}
				if(talbai_ll.isShown())
				{
					if(!talbai1.getText().toString().equals(""))
					{
						talbai1_t=talbai1_t+talbai1.getText().toString();
						url=url+"&"+talbai1_t;
					}
					if(!talbai2.getText().toString().equals(""))
					{
						talbai2_t=talbai2_t+talbai2.getText().toString();
						url=url+"&"+talbai2_t;
					}
				}
				if(!price1.getText().toString().equals(""))
				{
					minprice=minprice+price1.getText().toString();
					url=url+"&"+minprice;
				}
				if(!price2.getText().toString().equals(""))
				{
					maxprice=maxprice+price2.getText().toString();
					url=url+"&"+maxprice;
				}
				if(yavts_ll.isShown())
				{
					if(!yavts.getText().toString().equals(""))
					{
						yavts_t=yavts_t+yavts.getText().toString();
						url=url+"&"+yavts_t;
					}
					else
					{
						yavts_t=yavts_t+"0";
						url=url+"&"+yavts_t;
					}
				}
				else
				{
					yavts_t=yavts_t+"0";
					url=url+"&"+yavts_t;
				}
				if(type_ll.isShown())
				{
					type_t=type_t+(type.getSelectedItemPosition()+1);
					url=url+"&"+type_t;
				}
				duureg_t=duureg_t+duureg.getSelectedItemPosition();
				url=url+"&"+duureg_t;
				url=url+"&"+"page=1";
				Intent intent = new Intent(SearchActivity.this, ItemActivity.class);
				Bundle bundle = new Bundle();
				if(category.getSelectedItemPosition()==0)
					bundle.putInt("title", R.id.title_menu_1);
				else if(category.getSelectedItemPosition()==1)
					bundle.putInt("title", R.id.title_menu_2);
				else if(category.getSelectedItemPosition()==2)
					bundle.putInt("title", R.id.title_menu_3);
				bundle.putString("URL", url);
				intent.putExtras(bundle);
				startActivity(intent);
				finish();
				overridePendingTransition(R.anim.top_out, R.anim.right_in);
			}
		});
	}
	@Override
	public void onBackPressed() {
	   finish();
	   overridePendingTransition(R.anim.no_anim, R.anim.top_out);
	}

}
